/*
 * 2013 Manuel Dahmen
 */

package info.emptycanvas.library.testing;


import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;



import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

import info.emptycanvas.library.object.ECBufferedImage;
import info.emptycanvas.library.testing.TestObjet.ImageContainer;

/**
 * 
 * @author Manuel DAHMEN
 */
public class ShowTestResult extends javax.swing.JFrame implements Runnable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7844993762133687210L;
	private ECBufferedImage image = null;
	private ImageContainer biic;
	private boolean stop  =false;

	/**
	 * Creates new form ShowTestResult
	 */
	public ShowTestResult() {
		initComponents();
	}

	public ShowTestResult(BufferedImage ri) {
		initComponents();

		loadImage(ri);

		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void loadImage(BufferedImage ri) {
		this.image = new ECBufferedImage(ri);
		if (image != null) {
			setSize(new Dimension(image.getWidth(), image.getHeight()));
		}
	}

	public void setImageContainer(TestObjet.ImageContainer ic) {
		this.biic = ic;
	}

	public ShowTestResult(ECBufferedImage ri) {
		initComponents();

		image = ri;

		if (image != null) {
			setSize(new Dimension(image.getWidth(), image.getHeight()));
		}
		setVisible(true);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
		
		addKeyListener(new KeyListener()
		{

			private boolean stop;

			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyChar()=='P' || e.getKeyChar()=='p')
				{
					if(testRef!=null && testRef instanceof Runnable)
					{
						testRef.PAUSE();
					}
				}
				if(e.getKeyChar()=='S' || e.getKeyChar()=='s')
				{
					if(testRef!=null && testRef instanceof Runnable)
					{
						testRef.STOP();
						stop = true;
						dispose();
					}
				}
				
			}

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});
	}

	@Override
	public void run() {
		while (true && !stop  ) {
			dessine();
			try {
				Thread.sleep(100);
			} catch (InterruptedException ex) {
				Logger.getLogger(ShowTestResult.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}
	}

	public void dessine() {
		if (biic != null && biic.getImage() != null) {
			image = new ECBufferedImage(biic.getImage());
			if (image != null) {
				if (jPanel1 != null) {
					Graphics g = jPanel1.getGraphics();
					if (g != null) {
						jPanel1.getGraphics().drawImage(image, 0, 0,
								jPanel1.getWidth(), jPanel1.getHeight(), 0, 0,
								image.getWidth(), image.getHeight(), null);
						// jPanel1.getGraphics().setColor(Color.red);
						// jPanel1.getGraphics().drawRect(0, 0, 400, 200);
						jPanel1.getGraphics().drawString(biic.getStr(), 10, 10);
						jPanel1.getGraphics().drawString(" ? Pause ? "+testRef.isPause()+" ? Pause active ? "+testRef.isPauseActive(), 50, 10);
						
					}
				}
			}
		}
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setBackground(new java.awt.Color(204, 204, 255));

        jPanel1.setBackground(new java.awt.Color(204, 255, 204));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 380, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 278, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
	private TestObjet testRef;
	private Throwable throwable;

	public void setTestObjet(TestObjet testObjet) {
		this.testRef = testObjet;
		setTitle(testObjet.getClass().getCanonicalName());
		
	}
	public void exceptionReception(Exception t)
	{
		this.throwable = t;
		try {
			image = new ECBufferedImage(
ImageIO.read(					
					getClass().getResourceAsStream("be/ibiiztera/md/pmatrix/test/pushmatrix/newtest/skull-cross-bones-evil.ico")
			)		
			);
		} catch (IOException e) {
			
		}
		
	}

    void stopThreads() {
        stop = true;
    }
}
